﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using LoA.Shared.Interfaces;
using LoA.Shared.Physics;
using LoA.Shared;

namespace LoA.CharacterEngine {
	public partial class CharacterComponent : DrawableGameComponent, ICharacter {
		private static CharacterComponent _current = null;
		public static CharacterComponent Current { get { return _current; } }

		readonly IInput Input;
		readonly ICamera Camera;
		readonly IMap Map;
		readonly IPhysics Physics;

		public CharacterComponent(Game game, IInput input, ICamera camera, IMap map, IPhysics physics)
			: base(game) {
			if (_current == null) {
				_current = this;
			}
			Input = input;
			Camera = camera;
			Map = map;
			Physics = physics;
		}

		private PhysicalBody character;

		public Vector3 Position { get { return (this.character != null ? this.character.BodyState.Position : Vector3.Zero); } }
		public Matrix Orientation { get { return (this.character != null ? this.character.BodyState.Orientation : Matrix.Identity); } }

		public override void Initialize( ) {
			this.Suspended = false;

			base.Initialize( );
		}

		protected override void LoadContent( ) {
			var position = Vector3.Zero;
			var orientation = Matrix.Identity;
			var bodyModel = Game.Content.Load<Model>(@"Model\dude-1");
			var anim = Game.Content.Load<AnimationStructure>(@"Model\dude-walk.animation");

			character = new PhysicalBody(bodyModel, position, orientation, anim);

			this.Physics.AddObject(this.character);

			Map.UpdatePosition(this.Position);
			Camera.UpdateTarget(this.Position, this.Orientation);

			base.LoadContent( );
		}
		protected override void UnloadContent( ) {
			base.UnloadContent( );
		}

		public bool Suspended { get; private set; }
		public void Suspend( ) { this.Suspended = true; }
		public void Resume( ) { this.Suspended = false; }
	}
}